﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;
namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucHienThiNhanVien : UserControl
    {

        String ID_Co;
        public ucHienThiNhanVien()
        {
            InitializeComponent();
            Initialize();
            ShowData();
            this.cmKhoa.SelectedIndexChanged += new System.EventHandler(this.cmKhoa_SelectedIndexChanged);
        }

        void LoadAllKhoa()
        {
                
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        void Initialize()
        {

            KhoaBUO khoaobj = new KhoaBUO();
            cmKhoa.DataSource=khoaobj.GetAll();
            cmKhoa.DisplayMember = "WardName";
            cmKhoa.ValueMember = "id_Ward";

            // Update Gioi tinh
            ComboboxItem item1 = new ComboboxItem("Nam", 1);
            ComboboxItem item2 = new ComboboxItem("Nữ", 2);
            List<ComboboxItem> gt = new List<ComboboxItem>();
            gt.Add(item1);
            gt.Add(item2);

            cbGioiTinh.DataSource = gt;
            cbGioiTinh.DisplayMember = "Text";
            cbGioiTinh.ValueMember = "Text";

            //Update Vi tri
            PositionBUO posobj = new PositionBUO();
            cbVitri.DataSource = posobj.GetAll();
            cbVitri.DisplayMember = "PositionName";
            cbVitri.ValueMember = "PositionId";

            // Update Pay week and monthe
            ComboboxItem item12 = new ComboboxItem("Week", 1);
            ComboboxItem item22 = new ComboboxItem("Month", 2);
            List<ComboboxItem> wm = new List<ComboboxItem>();
            wm.Add(item12);
            wm.Add(item22);

            cbPay.DataSource = wm;
            cbPay.DisplayMember = "Text";
            cbPay.ValueMember = "Text";


            // update Loai hop dong
            ComboboxItem item13 = new ComboboxItem("P", 1);
            ComboboxItem item23 = new ComboboxItem("T", 2);
            List<ComboboxItem> pt = new List<ComboboxItem>();
            pt.Add(item13);
            pt.Add(item23);
            cbHopDong.DataSource = pt;

            cbHopDong.DisplayMember = "Text";
            cbHopDong.ValueMember = "Text";


        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            PositionBUO pb = new PositionBUO();
            tbl_Position p = new tbl_Position();
            p.PositionName = cbVitri.Text;
            if (pb.Insert(p))
            {
                MessageBox.Show("Thành công!");
            }
            else
            {
                MessageBox.Show("Thất bại!");
            }
            PositionBUO posobj = new PositionBUO();
            cbVitri.DataSource = posobj.GetAll();
            cbVitri.DisplayMember = "PositionName";
            cbVitri.ValueMember = "PositionId";

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tbl_NhanVien obj = new tbl_NhanVien();
            NhanVienBUO nv = new NhanVienBUO();

            obj.FirstName = txtHo.Text;
            obj.LastName = txtten.Text;
            obj.KhoaID = int.Parse(cmKhoa.SelectedValue.ToString());
            obj.Address = txtDiaChi.Text;
            obj.Sex =((ComboboxItem)cbGioiTinh.SelectedItem).Text.ToString();
            obj.DateOfBirth = dateTimePicker1.Text;
            obj.Tel = txtTel.Text;
            obj.NIN = txtBHYT.Text;
            obj.Position = int.Parse(cbVitri.SelectedValue.ToString());
            obj.CurrentSalary = txtLuongHT.Text;
            obj.SalaryScale = txtBacLuong.Text;
            obj.HoursPerWeek = txtsh.Text;
            obj.PaidWeeklyOrMonthly = ((ComboboxItem)cbPay.SelectedItem).Text.ToString();
            obj.PermanentOrTemporary = ((ComboboxItem)cbHopDong.SelectedItem).Text.ToString();

            if (nv.Insert(obj))
            {
                MessageBox.Show("Thêm thành công");
            }
            else
            {
                MessageBox.Show("Thêm thất bại");
            }

            ShowData();

                


        }
        void ShowData()
        {
            NhanVienBUO nv = new NhanVienBUO();

            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = nv.GetAll();


        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co = dataGridView1.Rows[hit.RowIndex].Cells[0].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null;





            }
        }


        private void xemChiTiếtToolStripMenuItem_Click(object sender, EventArgs e)
        {

            NhanVienBUO nv = new NhanVienBUO();
            tbl_NhanVien obj = nv.GetSingle(int.Parse(ID_Co));


             txtHo.Text=obj.FirstName ;
             txtten.Text=obj.LastName;
             cmKhoa.SelectedValue = obj.KhoaID;
             txtDiaChi.Text = obj.Address;             
             cbGioiTinh.SelectedValue = obj.Sex;
             dateTimePicker1.Text= obj.DateOfBirth;
           
            txtTel.Text=obj.Tel;
            txtBHYT.Text=obj.NIN ;
            
            cbVitri.SelectedValue = obj.Position;
            txtLuongHT.Text= obj.CurrentSalary ;
             txtBacLuong.Text=obj.SalaryScale ;
             txtsh.Text=obj.HoursPerWeek ;           
            cbPay.SelectedValue = obj.PaidWeeklyOrMonthly;
            cbHopDong.SelectedValue = obj.PermanentOrTemporary;

                        
                        
            

        }

        private void cmKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {



          
                NhanVienBUO nv = new NhanVienBUO();

                dataGridView1.AutoGenerateColumns = false;

                tbl_Khoa t = (tbl_Khoa)cmKhoa.SelectedItem;

                dataGridView1.DataSource = nv.GetAllNhanVienByKhoa(int.Parse(t.id_Ward.ToString()));
               
              
            

            
               
                    
            
           

        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NhanVienBUO nv = new NhanVienBUO();
            tbl_NhanVien obj = nv.GetSingle(int.Parse(ID_Co));


            txtHo.Text = obj.FirstName;
            txtten.Text = obj.LastName;
            cmKhoa.SelectedValue = obj.KhoaID;
            txtDiaChi.Text = obj.Address;
            cbGioiTinh.SelectedValue = obj.Sex;
            dateTimePicker1.Text = obj.DateOfBirth;

            txtTel.Text = obj.Tel;
            txtBHYT.Text = obj.NIN;

            cbVitri.SelectedValue = obj.Position;
            txtLuongHT.Text = obj.CurrentSalary;
            txtBacLuong.Text = obj.SalaryScale;
            txtsh.Text = obj.HoursPerWeek;
            cbPay.SelectedValue = obj.PaidWeeklyOrMonthly;
            cbHopDong.SelectedValue = obj.PermanentOrTemporary;
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NhanVienBUO nv = new NhanVienBUO();
            

            DialogResult ds = MessageBox.Show("Co nen xoa hay khong", "Xoa du lieu", MessageBoxButtons.YesNo);
            if (ds == DialogResult.Yes)
            {
                if (nv.Delete(int.Parse(ID_Co)))
                {
                    MessageBox.Show("Xoa thanh cong");
                }
                else
                {
                    MessageBox.Show("Xoa that bai");
                }
            }
            else
            {

            }

            ShowData();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            NhanVienBUO nv = new NhanVienBUO();

            dataGridView1.AutoGenerateColumns = false;
          
           // dataGridView1.DataSource = nv.SearchNhanVien(txtTenNhanVien.Text);
            dataGridView1.Refresh();

        }

        private void bằngCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ucBangCap bc = new ucBangCap(ID_Co);
            ControlAcess.ShowConTrol(this.ParentForm, bc, "gbHienThi");


        }

     

     
       
    }
}
